<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="limits" uri="/WEB-INF/tld/limits.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>行业模板统计</title>
<link rel="stylesheet" type="text/css"	href="../static/js/themes/defalut.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/public.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/ddspstyle.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/icon.css">
<link rel="stylesheet" type="text/css"	href="../static/js/WdatePicker/skin/WdatePicker.css">
<script type="text/javascript" src="../static/js/jquery.min.js"></script>
<script type="text/javascript" src="../static/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../static/js/index.js"></script>
<script type="text/javascript" src="../static/js/WdatePicker/WdatePicker.js"></script>

<script type="text/javascript">
	var queryParams;
	
	function EasyuiDatagridCityAndUsers() {
		$("#easyui-datagrid").datagrid({
			url: 'selectTemplateStatisticsList.do',
			nowrap: true,
			striped: true,
			border: false,
			fit: true,
			rownumbers:true,
			queryParams: getGueryParams(),
			columns: [[
				{field:'f_cc_name',title:'客户账号',align:'center',width:150},
				{field:'f_po_address',title:'地市',align:'center',width:150},
				{field:'f_po_industry',title:'行业单位',align:'center',width:150},
				{field:'f_po_name',title:'单位名称',align:'center',width:300},
				{field:'f_po_model_name',title:'模板名称',align:'center',width:150},
				{field:'f_cc_hy',title:'是否活跃',align:'center',width:150}
			]],
			singleSelect:false,
			pagination:true,
			pageList:[10,20,30,40,50],
			pageSize:10,
			toolbar: '#toolbar',
			
			onExpandRow: function(index,row){
			
			},
			onSelect: function(rowIndex,rowData) {
				
			},
			onUnselect: function(rowIndex,rowData) {
				
			},
			onCheckAll: function(rowIndex,rowData) {
				
			},
			onUncheckAll: function(rowIndex,rowData) {
			
			},
			onLoadSuccess: function(data) {
				if(data.total==-1) {
					$.messager.defaults={ok:"确定",cancel:"取消"};
					$.messager.confirm('提示', '用户尚未登录,请登录', function(r){
					if (r){
					    window.parent.location.href='/login.do';
					    return false;
					}});
				}
			}
		});
	}
	
	function EasyuiDatagridTemplateActiveStatistics() {
		$("#easyui-datagrid").datagrid({
			url: 'selectTemplateStatisticsList.do',
			nowrap: true,
			striped: true,
			border: false,
			fit: true,
			rownumbers:true,
			queryParams: getGueryParams(),
			columns: [[
				{field:'current_Month_active_template',title:'本月活跃模板',align:'center',width:150},
				{field:'last_Month_active_template',title:'上月活跃模板',align:'center',width:150}
			]],
			singleSelect:false,
			pagination:true,
			pageList:[10,20,30,40,50],
			pageSize:10,
			toolbar: '#toolbar',
			
			onExpandRow: function(index,row){
			
			},
			onSelect: function(rowIndex,rowData) {
				
			},
			onUnselect: function(rowIndex,rowData) {
				
			},
			onCheckAll: function(rowIndex,rowData) {
				
			},
			onUncheckAll: function(rowIndex,rowData) {
			
			},
			onLoadSuccess: function(data) {
				if(data.total==-1) {
					$.messager.defaults={ok:"确定",cancel:"取消"};
					$.messager.confirm('提示', '用户尚未登录,请登录', function(r){
					if (r){
					    window.parent.location.href='/login.do';
					    return false;
					}});
				}
			}
		});
	}
	
	function EasyuiDatagridTemplateSilenceStatistics() {
		$("#easyui-datagrid").datagrid({
			url: 'selectTemplateStatisticsList.do',
			nowrap: true,
			striped: true,
			border: false,
			fit: true,
			rownumbers:true,
			queryParams: getGueryParams(),
			columns: [[
				{field:'current_Month_silence_template',title:'本月沉默模板',align:'center',width:150},
				{field:'last_Month_silence_template',title:'上月沉默模板',align:'center',width:150}
			]],
			singleSelect:false,
			pagination:true,
			pageList:[10,20,30,40,50],
			pageSize:10,
			toolbar: '#toolbar',
			
			onExpandRow: function(index,row){
			
			},
			onSelect: function(rowIndex,rowData) {
				
			},
			onUnselect: function(rowIndex,rowData) {
				
			},
			onCheckAll: function(rowIndex,rowData) {
				
			},
			onUncheckAll: function(rowIndex,rowData) {
			
			},
			onLoadSuccess: function(data) {
				if(data.total==-1) {
					$.messager.defaults={ok:"确定",cancel:"取消"};
					$.messager.confirm('提示', '用户尚未登录,请登录', function(r){
					if (r){
					    window.parent.location.href='/login.do';
					    return false;
					}});
				}
			}
		});
	}
	
	function getGueryParamsStatisticsCount(){
		var replaceMap = {};
		var lastMonthObj = $("#selectDate").val();
		if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
			var lastMonth = getLastMonth();
			var currentMonth = getCurrentMonth(lastMonth);
			replaceMap['lastMonth']=lastMonth;
			replaceMap['currentMonth']=currentMonth;
	    }else{
	    	var currentMonthObj = getCurrentMonth(lastMonthObj);
	    	replaceMap['lastMonth']=lastMonthObj;
			replaceMap['currentMonth']=currentMonthObj;
	   }
		
		return {
			dynamicSqlCountKey:"local_active_users_statistics_count",
			dynamicSqlListKey:"",
			page:0,
			rows:10,
			replaceParam:replaceMap
		};
	}
	
	function getGueryParams(){
		
		if($("#statisticalDimension").val()==1){
			
			var replaceMap = {};
			var lastMonthObj = $("#selectDate").val();
			if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
				var lastMonth = getLastMonth();
				var currentMonth = getCurrentMonth(lastMonth);
				replaceMap['lastMonth']=lastMonth;
				replaceMap['currentMonth']=currentMonth;
		    }else{
		    	var currentMonthObj = getCurrentMonth(lastMonthObj);
		    	replaceMap['lastMonth']=lastMonthObj;
				replaceMap['currentMonth']=currentMonthObj;
		   }
			
			return {
				dynamicSqlCountKey:"local_active_users_statistics_count",
				dynamicSqlListKey:"local_active_users_statistics_list",
				replaceParam:replaceMap
			};
			
		}else if($("#statisticalDimension").val()==2){
			
			var replaceMap = {};
			var lastMonthObj = $("#selectDate").val();
			if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
				var lastMonth = getLastMonth();
				var currentMonth = getCurrentMonth(lastMonth);
				var lastTwoMonth = getLastTwoMonth(lastMonth);
				replaceMap['lastTwoMonth']=lastTwoMonth;
				replaceMap['lastMonth']=lastMonth;
				replaceMap['currentMonth']=currentMonth;
		    }else{
		    	var currentMonthObj = getCurrentMonth(lastMonthObj);
		    	var lastTwoMonthObj = getLastTwoMonth(lastMonthObj);
		    	replaceMap['lastTwoMonth']=lastTwoMonthObj;
		    	replaceMap['lastMonth']=lastMonthObj;
				replaceMap['currentMonth']=currentMonthObj;
		   }
			
			return {
				dynamicSqlCountKey:"",
				dynamicSqlListKey:"template_active_count",
				replaceParam:replaceMap
			};
			
		}else if($("#statisticalDimension").val()==3){
			
			var replaceMap = {};
			var lastMonthObj = $("#selectDate").val();
			if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
				var lastMonth = getLastMonth();
				var currentMonth = getCurrentMonth(lastMonth);
				var lastTwoMonth = getLastTwoMonth(lastMonth);
				replaceMap['lastTwoMonth']=lastTwoMonth;
				replaceMap['lastMonth']=lastMonth;
				replaceMap['currentMonth']=currentMonth;
		    }else{
		    	var currentMonthObj = getCurrentMonth(lastMonthObj);
		    	var lastTwoMonthObj = getLastTwoMonth(lastMonthObj);
		    	replaceMap['lastTwoMonth']=lastTwoMonthObj;
		    	replaceMap['lastMonth']=lastMonthObj;
				replaceMap['currentMonth']=currentMonthObj;
		   }
			
			return {
				dynamicSqlCountKey:"",
				dynamicSqlListKey:"template_silence_count",
				replaceParam:replaceMap
			};
			
		}
		
	}
	
	$(function(){
		
		var lastMonth = getLastMonth();
		$("#selectDate").val(lastMonth);
		
		$("#selectTemplateStatisticsExcelListIds").html("正在加载导出按钮.....");
		var str ="";
		if($("#statisticalDimension").val()==1){
			<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
				$.ajax({
					url:"selectTemplateStatisticsList.do",
					data:getGueryParamsStatisticsCount(),
					dataType: "json",
					type: "POST",
					success:function(data) {
							if(data.total>50000){
								var pageCount = Math.ceil(data.total/50000);
								for(var i=0;i<pageCount;i++){
									if(i==pageCount-1){
										var startData = i*50000;
										var endData = data.total-i*50000;
										str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">剩余的数据</span></span></a>';
									}else{
										var startData = i*50000;
										var endData = 50000;
										var exportStartData = "";
										if(i==0){
											exportStartData = "1"
										}else{
											exportStartData = (i*5)+"万"
										}
										var exportEndData = ((i+1)*5)+"万";
										str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">'+exportStartData+'到'+exportEndData+'的数据</span></span></a>';
									}
									
								}
								$("#selectTemplateStatisticsExcelListIds").html(str);
							}
					}
				});
			</limits:uiAuth>
			
		}else if($("#statisticalDimension").val()==2){
			<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
				str = '<a class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcel();" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">导出Excle</span></span></a>';
			</limits:uiAuth>
			$("#selectTemplateStatisticsExcelListIds").html(str);
		}else if($("#statisticalDimension").val()==3){
			<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
				str = '<a class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcel();" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">导出Excle</span></span></a>';
			</limits:uiAuth>
			$("#selectTemplateStatisticsExcelListIds").html(str);
		}
		
		
		gotoSearch = function(){
			
			if($("#statisticalDimension").val()==1){
				new EasyuiDatagridCityAndUsers();
			}else if($("#statisticalDimension").val()==2){
				new EasyuiDatagridTemplateActiveStatistics();
			}else if($("#statisticalDimension").val()==3){
				new EasyuiDatagridTemplateSilenceStatistics();
			}
			
		};
	});
	
	function load(){
		$(".datagrid-row-expander datagrid-row-expand").attr("display","none"); 
	}
	
	function updateStatisticalDimension(){
		$("#selectTemplateStatisticsExcelListIds").html("正在加载导出按钮.....");
		var str ="";
		if($("#statisticalDimension").val()==1){
			<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
				$.ajax({
					url:"selectTemplateStatisticsList.do",
					data:getGueryParamsStatisticsCount(),
					dataType: "json",
					type: "POST",
					success:function(data) {
							if(data.total>50000){
								var pageCount = Math.ceil(data.total/50000);
								for(var i=0;i<pageCount;i++){
									if(i==pageCount-1){
										var startData = i*50000;
										var endData = data.total-i*50000;
										str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">剩余的数据</span></span></a>';
									}else{
										var startData = i*50000;
										var endData = 50000;
										var exportStartData = "";
										if(i==0){
											exportStartData = "1"
										}else{
											exportStartData = (i*5)+"万"
										}
										var exportEndData = ((i+1)*5)+"万";
										str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">'+exportStartData+'到'+exportEndData+'的数据</span></span></a>';
									}
									
								}
								$("#selectTemplateStatisticsExcelListIds").html(str);
							}
					}
				});
			</limits:uiAuth>
			
		}else if($("#statisticalDimension").val()==2){
			<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
				str = '<a class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcel();" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">导出Excle</span></span></a>';
			</limits:uiAuth>
			$("#selectTemplateStatisticsExcelListIds").html(str);
		}else if($("#statisticalDimension").val()==3){
			<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
				str = '<a class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcel();" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">导出Excle</span></span></a>';
			</limits:uiAuth>
			$("#selectTemplateStatisticsExcelListIds").html(str);
		}
	}
	
	function exportExcelStatisticsList(e){
		
		var ALabel = $(e);
		var firstResult = ALabel.attr("startData");
		var maxResults = ALabel.attr("endData");
		
		var dynamicSqlCountKey="local_active_users_statistics_count";
		var dynamicSqlListKey="local_active_users_statistics_excel_list";
		
		var replaceMap = {};
		var lastMonthObj = $("#selectDate").val();
		if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
			var lastMonth = getLastMonth();
			var currentMonth = getCurrentMonth(lastMonth);
			replaceMap['lastMonth']=lastMonth;
			replaceMap['currentMonth']=currentMonth;
	    }else{
	    	var currentMonthObj = getCurrentMonth(lastMonthObj);
	    	replaceMap['lastMonth']=lastMonthObj;
			replaceMap['currentMonth']=currentMonthObj;
		}
		replaceMap['firstResult']=firstResult;
		replaceMap['maxResults']=maxResults;
		
		var titleMap = {};
		titleMap['f_cc_name']=encodeURIComponent("客户账号");
		titleMap['f_po_address']=encodeURIComponent("地市");
		titleMap['f_po_industry']=encodeURIComponent("行业单位");
		titleMap['f_po_name']=encodeURIComponent("单位名称");
		titleMap['f_po_model_name']=encodeURIComponent("模板名称");
		titleMap['f_cc_hy']=encodeURIComponent("是否活跃");
		
		var mapJson=JSON.stringify(replaceMap);
		var titleJson=JSON.stringify(titleMap);
		titleJson=encodeURIComponent(titleJson);
		mapJson=encodeURIComponent(mapJson);
		var url="selectTemplateStatisticsExcelList.do?dynamicSqlCountKey="+dynamicSqlCountKey+"&dynamicSqlListKey="+dynamicSqlListKey+"&replaceJson="+mapJson+"&titleJson="+titleJson;
        window.open(url);
		
	}
	
	function exportExcel(){
		
		if($("#statisticalDimension").val()==2){
			
			var dynamicSqlCountKey="";
			var dynamicSqlListKey="template_active_count";
			
			var replaceMap = {};
			var lastMonthObj = $("#selectDate").val();
			if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
				var lastMonth = getLastMonth();
				var currentMonth = getCurrentMonth(lastMonth);
				var lastTwoMonth = getLastTwoMonth(lastMonth);
				replaceMap['lastTwoMonth']=lastTwoMonth;
				replaceMap['lastMonth']=lastMonth;
				replaceMap['currentMonth']=currentMonth;
		    }else{
		    	var currentMonthObj = getCurrentMonth(lastMonthObj);
		    	var lastTwoMonthObj = getLastTwoMonth(lastMonthObj);
		    	replaceMap['lastTwoMonth']=lastTwoMonthObj;
		    	replaceMap['lastMonth']=lastMonthObj;
				replaceMap['currentMonth']=currentMonthObj;
		   }
			
			var titleMap = {};
			titleMap['current_Month_active_template']=encodeURIComponent("本月活跃模板");
			titleMap['last_Month_active_template']=encodeURIComponent("上月活跃模板");
			
			var mapJson=JSON.stringify(replaceMap);
			var titleJson=JSON.stringify(titleMap);
			titleJson=encodeURIComponent(titleJson);
			mapJson=encodeURIComponent(mapJson);
			var url="selectTemplateStatisticsExcelList.do?dynamicSqlCountKey="+dynamicSqlCountKey+"&dynamicSqlListKey="+dynamicSqlListKey+"&replaceJson="+mapJson+"&titleJson="+titleJson;
	        window.open(url);
		}else if($("#statisticalDimension").val()==3){
			
			var dynamicSqlCountKey="";
			var dynamicSqlListKey="template_silence_count";
			
			var replaceMap = {};
			var lastMonthObj = $("#selectDate").val();
			if(typeof lastMonthObj == "undefined" || lastMonthObj == null || lastMonthObj == ""){
				var lastMonth = getLastMonth();
				var currentMonth = getCurrentMonth(lastMonth);
				var lastTwoMonth = getLastTwoMonth(lastMonth);
				replaceMap['lastTwoMonth']=lastTwoMonth;
				replaceMap['lastMonth']=lastMonth;
				replaceMap['currentMonth']=currentMonth;
		    }else{
		    	var currentMonthObj = getCurrentMonth(lastMonthObj);
		    	var lastTwoMonthObj = getLastTwoMonth(lastMonthObj);
		    	replaceMap['lastTwoMonth']=lastTwoMonthObj;
		    	replaceMap['lastMonth']=lastMonthObj;
				replaceMap['currentMonth']=currentMonthObj;
		   }
			
			var titleMap = {};
			titleMap['current_Month_silence_template']=encodeURIComponent("本月沉默模板");
			titleMap['last_Month_silence_template']=encodeURIComponent("上月沉默模板");
			
			var mapJson=JSON.stringify(replaceMap);
			var titleJson=JSON.stringify(titleMap);
			titleJson=encodeURIComponent(titleJson);
			mapJson=encodeURIComponent(mapJson);
			var url="selectTemplateStatisticsExcelList.do?dynamicSqlCountKey="+dynamicSqlCountKey+"&dynamicSqlListKey="+dynamicSqlListKey+"&replaceJson="+mapJson+"&titleJson="+titleJson;
			window.open(url);
			
		}
		
	}

	function getLastMonth(){//获取上个月日期
	    var date = new Date; 
	    var year = date.getFullYear();
	    var month = date.getMonth();
	    if(month == 0){
	         year = year -1;
	         month = 12; 
	    }
		month =(month<10 ? "0"+month:month);
	    return year+"-"+month;
	}
	
	function getCurrentMonth(lastMonth){//获取当前月
		
		var date = new Date(lastMonth);
	    date.setMonth(date.getMonth() + 1);
	    
		// 没有格式化的功能，只能一个一个取
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		month =(month<10 ? "0"+month:month);
		return year+"-"+month;
	
	}
	
	function getLastTwoMonth(lastMonth){//获取上2个月
		
		var date = new Date(lastMonth);
	    date.setMonth(date.getMonth() - 1);
	    
		// 没有格式化的功能，只能一个一个取
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		month =(month<10 ? "0"+month:month);
		return year+"-"+month;
	
	}
	
</script>
</head>
<body onload="load();">
	<table id="easyui-datagrid" title="行业模板统计"></table>
	<div id="toolbar" class="easyui_datagrid_toolbar" style="">
		<div class="easyuiSearch">
			<form onsubmit="return false;" method="post">
				<table cellspacing="0" cellpadding="0" width="100%">
					
					<tr>
						<td align="left" width="7%">
							统计维度：
						</td>
						<td align="left" width="15%">
							<select name="statisticalDimension" id="statisticalDimension" style="width: 120px;" onchange="updateStatisticalDimension()">
								<option value="1" selected="selected">地市用户统计</option>
								<option value="2" >活跃模板数统计</option>
								<option value="3" >沉默模板数统计</option>
							</select>
						</td>
						<td align="left" width="8%">
							查询月份：
						</td>
						<td align="left" width="15%">
							<input id="selectDate" name="selectDate" type="text" onClick="WdatePicker({dateFmt:'yyyy-MM'})" style="width: 150px;" value="" class="yl_inp yw_name" />	
						</td>
						
						<td align="left" width="6%">
							<limits:uiAuth url="selectTemplateStatisticsList.do">
								<a href="javascript:void(0);" class="easyui-linkbutton"
									data-options="iconCls:'icon-search', plain:true"
									onclick="gotoSearch($('#easyui-datagrid'))">查询</a>
							</limits:uiAuth>
						</td>
						
						<td align="left" id="selectTemplateStatisticsExcelListIds">
							<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
								正在加载导出按钮.....
							</limits:uiAuth>
						</td>
						
					</tr>
					
				</table>
			</form>
		</div>
	</div>
	
</body>
</html>